SCENARIO( "Testing the unionization of the energy Grid" ){
  // These are the same regardless of the resonance formalism
  std::set< double > userSupplied{ 1.0, 2.0, 3.0 };
  auto energies = XSEnergies();
  WHEN( "resonances have not been reconstructed" ){
    
    GIVEN( "a linearized ResonanceReconstructionDataDelivery (SLBW) object" ){
      auto material = details::ENDFMaterial( "SLBW" );
      auto r2d2 = njoy::RECONR::R2D2::Factory()( logger, material );
    
      double absTolerance{ 1E-6 };
      double relTolerance{ 1E-1 }; // This tolerance is large by design
      tRECONR::linearizeXS( logger, r2d2, absTolerance, relTolerance );
    
      THEN( "the energygrid can be unionized" ){
        std::vector< double > refGrid{
          1e-05,        1.0625e-05,   1.125e-05,    1.1875e-05,   1.25e-05,     
          1.3125e-05,   1.375e-05,    1.4375e-05,   1.5e-05,      1.5625e-05,   
          1.625e-05,    1.75e-05,     1.875e-05,    1.9375e-05,   1.96875e-05,  
          1.98438e-05,  1.99219e-05,  1.99609e-05,  1.99805e-05,  1.99902e-05,  
          1.99951e-05,  1.99976e-05,  1.99988e-05,  1.99994e-05,  1.99997e-05,  
          1.99998e-05,  1.99999e-05,  2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        6.36557e-05,  0.000107311,  
          0.000194623,  0.000369246,  0.000718492,  0.00141698,   0.00281397,   
          0.00560794,   0.0111959,    0.0223717,    0.0447235,    0.089427,     
          0.1,          0.178834,     0.357648,     0.715276,     0.986069,     
          1,            1.0253,       1.06453,      1.43053,      2,            
          2.86104,      2.98607,      3,            3.0253,       3.06453,      
          5.72207,      7.5,          11.4441,      22.8882,      45.7764,      
          91.5528,      183.105,      366.211,      732.422,      1464.84,      
          2929.69,      5859.38,      11718.8,      15000,        17000,        
          20000,        23437.5,      25000,        30000,        35000,        
          40000,        46875,        50000,        60000,        70000,        
          80000,        90000,        93750,        100000,       125000,       
          137500,       143750,       146875,       148438,       149219,       
          149609,       149805,       149902,       149951,       149976,       
          149988,       149994,       149997,       149998,       149999,       
          150000,       150000,       150000,       150000,       150000,       
          150000,       150000,       150000,       150000,       150000,       
          187500,       375000,       450000,       562500,       600000,       
          656250,       675000,       703125,       712500,       726562,       
          731250,       738281,       740625,       744141,       745312,       
          747070,       747656,       748535,       748828,       749268,       
          749414,       749634,       749707,       749817,       749854,       
          749908,       749927,       749954,       749963,       749977,       
          749982,       749989,       749991,       749994,       749995,       
          749997,       749998,       749999,       749999,       749999,       
          749999,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       1.09e+06,     2e+06,        
          5e+06,        5.5e+06,      6e+06,        6.5e+06,      7e+06,        
          7.5e+06,      8e+06,        1.42e+07,     1.9e+07,      1.95e+07,     
          2e+07,        3e+07
        };
    
        auto trial = tRECONR::unionizeEnergyGrid( logger, r2d2, userSupplied );
    
        details::checkRanges( refGrid, trial );
      } // THEN
    } // GIVEN
    GIVEN( "a linearized ResonanceReconstructionDataDelivery (RM) object" ){
      auto material = details::ENDFMaterial( "RM" );
      auto r2d2 = njoy::RECONR::R2D2::Factory()( logger, material );
    
      double absTolerance{ 1E-6 };
      double relTolerance{ 1E-1 }; // This tolerance is large by design
      tRECONR::linearizeXS( logger, r2d2, absTolerance, relTolerance );
    
      THEN( "the energygrid can be unionized" ){
        std::vector< double > refGrid { 
          1e-05,        1.0625e-05,   1.125e-05,    1.1875e-05,   1.25e-05,     
          1.3125e-05,   1.375e-05,    1.4375e-05,   1.5e-05,      1.5625e-05,   
          1.625e-05,    1.75e-05,     1.875e-05,    1.9375e-05,   1.96875e-05,  
          1.98438e-05,  1.99219e-05,  1.99609e-05,  1.99805e-05,  1.99902e-05,  
          1.99951e-05,  1.99976e-05,  1.99988e-05,  1.99994e-05,  1.99997e-05,  
          1.99998e-05,  1.99999e-05,  2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        6.36557e-05,  0.000107311,  
          0.000194623,  0.000369246,  0.000718492,  0.00141698,   0.00281397,   
          0.00560794,   0.0111959,    0.0223717,    0.0447235,    0.089427,     
          0.1,          0.178834,     0.357648,     0.715276,     0.98596,      
          1,            1.06389,      1.1389,       1.21391,      1.43053,      
          2,            2.02109,      2.0361,       2.05111,      2.74827,      
          2.7767,       2.80513,      2.86104,      3,            3.14517,      
          3.1566,       3.16803,      3.60111,      3.6208,       3.64049,      
          4.8336,       4.8508,       4.868,        5.24932,      5.4497,       
          5.5,          5.72207,      11.4441,      22.8882,      45.7764,      
          91.5528,      183.105,      366.211,      732.422,      1464.84,      
          2929.69,      5859.38,      11718.8,      15000,        17000,        
          20000,        23437.5,      25000,        30000,        35000,        
          40000,        46875,        50000,        60000,        70000,        
          80000,        90000,        93750,        100000,       125000,       
          137500,       143750,       146875,       148438,       149219,       
          149609,       149805,       149902,       149951,       149976,       
          149988,       149994,       149997,       149998,       149999,       
          150000,       150000,       150000,       150000,       150000,       
          150000,       150000,       150000,       150000,       150000,       
          187500,       375000,       450000,       562500,       600000,       
          656250,       675000,       703125,       712500,       726562,       
          731250,       738281,       740625,       744141,       745312,       
          747070,       747656,       748535,       748828,       749268,       
          749414,       749634,       749707,       749817,       749854,       
          749908,       749927,       749954,       749963,       749977,       
          749982,       749989,       749991,       749994,       749995,       
          749997,       749998,       749999,       749999,       749999,       
          749999,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       1.09e+06,     2e+06,        
          5e+06,        5.5e+06,      6e+06,        6.5e+06,      7e+06,        
          7.5e+06,      8e+06,        1.42e+07,     1.9e+07,      1.95e+07,     
          2e+07,        3e+07
        };
    
        auto trial = tRECONR::unionizeEnergyGrid( logger, r2d2, userSupplied );
    
        details::checkRanges( refGrid, trial );
      } // THEN
    } // GIVEN
    GIVEN( "a linearized ResonanceReconstructionDataDelivery (RML) object" ){
      auto material = details::ENDFMaterial( "RML" );
      auto r2d2 = njoy::RECONR::R2D2::Factory()( logger, material );
    
      double absTolerance{ 1E-6 };
      double relTolerance{ 1E-1 }; // This tolerance is large by design
      tRECONR::linearizeXS( logger, r2d2, absTolerance, relTolerance );
    
      THEN( "the energygrid can be unionized" ){
        std::vector< double > refGrid { 
          1e-05,        1.0625e-05,   1.125e-05,    1.1875e-05,   1.25e-05,     
          1.3125e-05,   1.375e-05,    1.4375e-05,   1.5e-05,      1.5625e-05,   
          1.625e-05,    1.75e-05,     1.875e-05,    1.9375e-05,   1.96875e-05,  
          1.98438e-05,  1.99219e-05,  1.99609e-05,  1.99805e-05,  1.99902e-05,  
          1.99951e-05,  1.99976e-05,  1.99988e-05,  1.99994e-05,  1.99997e-05,  
          1.99998e-05,  1.99999e-05,  2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        6.36557e-05,  0.000107311,  
          0.000194623,  0.000369246,  0.000718492,  0.00141698,   0.00281397,   
          0.00560794,   0.0111959,    0.0223717,    0.0447235,    0.089427,     
          0.1,          0.178834,     0.357648,     0.715276,     1,            
          1.43053,      2,            2.86104,      3,            5.72207,      
          11.4441,      22.8882,      45.7764,      91.5528,      183.105,      
          366.211,      732.422,      1464.84,      2929.69,      5859.38,      
          7193.6,       7788,         8382.4,       11718.8,      15000,        
          17000,        20000,        23437.5,      25000,        30000,        
          35000,        40000,        46875,        50000,        60000,        
          70000,        80000,        90000,        93750,        100000,       
          125000,       137500,       143750,       146875,       148438,       
          149219,       149609,       149805,       149902,       149951,       
          149976,       149988,       149994,       149997,       149998,       
          149999,       150000,       150000,       150000,       150000,       
          150000,       150000,       150000,       150000,       150000,       
          150000,       187500,       375000,       450000,       562500,       
          600000,       656250,       675000,       703125,       712500,       
          726562,       731250,       738281,       740625,       744141,       
          745312,       747070,       747656,       748535,       748828,       
          749268,       749414,       749634,       749707,       749817,       
          749854,       749908,       749927,       749954,       749963,       
          749977,       749982,       749989,       749991,       749994,       
          749995,       749997,       749998,       749999,       749999,       
          749999,       749999,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       1.09e+06,     
          2e+06,        5e+06,        5.5e+06,      6e+06,        6.5e+06,      
          7e+06,        7.5e+06,      8e+06,        1.42e+07,     1.9e+07,      
          1.95e+07,     2e+07,        3e+07
        };
    
        auto trial = tRECONR::unionizeEnergyGrid( logger, r2d2, userSupplied );
    
        details::checkRanges( refGrid, trial );
      } // THEN
    } // GIVEN
  } // WHEN

  WHEN( "resonances have been reconstructed" ){
    GIVEN( "a linearized ResonanceReconstructionDataDelivery (SLBW) object" ){
      auto material = details::ENDFMaterial( "SLBW" );
      auto r2d2 = njoy::RECONR::R2D2::Factory()( logger, material );
    
      double absTolerance{ 1E-6 };
      double relTolerance{ 1E-1 }; // This tolerance is large by design
      tRECONR::linearizeXS( logger, r2d2, absTolerance, relTolerance );
      auto refGrid = tRECONR::unionizeEnergyGrid( logger, r2d2, userSupplied );
      auto energies = refGrid;
      tRECONR::reconstructResonances( logger, refGrid, r2d2, 1E-1, 1E-3 );
    
      THEN( "the reconstructed energygrid can be unionized" ){

        std::vector< double > ref = {
          1e-05,        1e-05,        1.0625e-05,   1.125e-05,    1.1875e-05,   
          1.25e-05,     1.3125e-05,   1.375e-05,    1.4375e-05,   1.5e-05,      
          1.5625e-05,   1.625e-05,    1.75e-05,     1.875e-05,    1.9375e-05,   
          1.96875e-05,  1.98438e-05,  1.99219e-05,  1.99609e-05,  1.99805e-05,  
          1.99902e-05,  1.99951e-05,  1.99976e-05,  1.99988e-05,  1.99994e-05,  
          1.99997e-05,  1.99998e-05,  1.99999e-05,  2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        2e-05,        4.18279e-05,  
          6.36557e-05,  0.000107311,  0.000194623,  0.000369246,  0.000718492,  
          0.00141698,   0.00281397,   0.00560794,   0.0111959,    0.0223717,    
          0.0447235,    0.089427,     0.1,          0.178834,     0.357648,     
          0.536462,     0.625869,     0.715276,     0.782974,     0.850672,     
          0.884522,     0.918371,     0.95222,      0.969145,     0.986069,     
          1,            1.0253,       1.06453,      1.08741,      1.11028,      
          1.13316,      1.15603,      1.20178,      1.24753,      1.33903,      
          1.43053,      1.5729,       1.71527,      2,            2.21526,      
          2.43052,      2.53815,      2.64578,      2.75341,      2.80723,      
          2.86104,      2.8923,       2.92356,      2.95481,      2.98607,      
          3,            3.0253,       3.06453,      3.08529,      3.10605,      
          3.14758,      3.1891,       3.23063,      3.31367,      3.39672,      
          3.47977,      3.56282,      3.72891,      3.89501,      4.06111,      
          4.3933,       5.05768,      5.72207,      6.61103,      7.5,          
          7.5,          7.5,          11.4441,      22.8882,      45.7764,      
          91.5528,      183.105,      366.211,      732.422,      1464.84,      
          2929.69,      5859.38,      11718.8,      15000,        15000,        
          15000,        17000,        20000,        23437.5,      25000,        
          30000,        35000,        40000,        46875,        50000,        
          60000,        70000,        80000,        90000,        93750,        
          100000,       100000,       100000,       125000,       137500,       
          143750,       146875,       148438,       149219,       149609,       
          149805,       149902,       149951,       149976,       149988,       
          149994,       149997,       149998,       149999,       150000,       
          150000,       150000,       150000,       150000,       150000,       
          150000,       150000,       150000,       150000,       187500,       
          375000,       450000,       562500,       600000,       656250,       
          675000,       703125,       712500,       726562,       731250,       
          738281,       740625,       744141,       745312,       747070,       
          747656,       748535,       748828,       749268,       749414,       
          749634,       749707,       749817,       749854,       749908,       
          749927,       749954,       749963,       749977,       749982,       
          749989,       749991,       749994,       749995,       749997,       
          749998,       749999,       749999,       749999,       749999,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       1.09e+06,     2e+06,        5e+06,        
          5.5e+06,      6e+06,        6.5e+06,      7e+06,        7.5e+06,      
          8e+06,        1.42e+07,     1.9e+07,      1.95e+07,     2e+07,        
          3e+07
        };

        auto trial = tRECONR::unionizeEnergyGrid( logger, r2d2, energies );
        details::checkRanges( ref, trial );
      } // THEN
    } // GIVEN
    GIVEN( "a linearized ResonanceReconstructionDataDelivery (RM) object" ){
      auto material = details::ENDFMaterial( "RM" );
      auto r2d2 = njoy::RECONR::R2D2::Factory()( logger, material );
    
      double absTolerance{ 1E-6 };
      double relTolerance{ 1E-1 }; // This tolerance is large by design
      tRECONR::linearizeXS( logger, r2d2, absTolerance, relTolerance );
      auto refGrid = tRECONR::unionizeEnergyGrid( logger, r2d2, userSupplied );
      auto energies = refGrid;
      tRECONR::reconstructResonances( logger, refGrid, r2d2, 1E-1, 1E-3 );
    
      THEN( "the reconstructed energygrid can be unionized" ){

        std::vector< double > ref{
          1e-05,        1.0625e-05,   1.125e-05,    1.1875e-05,   1.25e-05,     
          1.3125e-05,   1.375e-05,    1.4375e-05,   1.5e-05,      1.5625e-05,   
          1.625e-05,    1.75e-05,     1.875e-05,    1.9375e-05,   1.96875e-05,  
          1.98438e-05,  1.99219e-05,  1.99609e-05,  1.99805e-05,  1.99902e-05,  
          1.99951e-05,  1.99976e-05,  1.99988e-05,  1.99994e-05,  1.99997e-05,  
          1.99998e-05,  1.99999e-05,  2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        2e-05,        2e-05,        
          2e-05,        2e-05,        2e-05,        6.36557e-05,  0.000107311,  
          0.000194623,  0.000369246,  0.000718492,  0.00141698,   0.00281397,   
          0.00560794,   0.0111959,    0.0223717,    0.0447235,    0.089427,     
          0.1,          0.178834,     0.357648,     0.715276,     0.98596,      
          0.98596,      1,            1.06389,      1.1389,       1.21391,      
          1.26807,      1.32222,      1.43053,      1.5729,       1.71527,      
          1.85763,      1.89322,      1.92882,      1.96441,      1.9822,       
          2,            2.02109,      2.0361,       2.05111,      2.0729,       
          2.09469,      2.11647,      2.13826,      2.18183,      2.2254,       
          2.31255,      2.39969,      2.57398,      2.66112,      2.70469,      
          2.74827,      2.7767,       2.80513,      2.86104,      2.93052,      
          3,            3.03629,      3.07258,      3.14517,      3.1566,       
          3.16803,      3.22217,      3.2763,       3.33044,      3.38457,      
          3.43871,      3.49284,      3.51991,      3.54698,      3.57404,      
          3.60111,      3.6208,       3.64049,      3.65913,      3.67778,      
          3.69642,      3.71506,      3.75234,      3.78963,      3.8642,       
          3.93877,      4.08791,      4.23704,      4.38618,      4.53532,      
          4.60989,      4.68446,      4.72174,      4.75903,      4.77767,      
          4.79631,      4.81496,      4.82428,      4.8336,       4.8508,       
          4.868,        4.87992,      4.89183,      4.90375,      4.91567,      
          4.9395,       4.96333,      5.011,        5.05866,      5.15399,      
          5.24932,      5.4497,       5.5,          5.5,          5.5,          
          5.72207,      11.4441,      22.8882,      45.7764,      91.5528,      
          183.105,      366.211,      732.422,      1464.84,      2929.69,      
          5859.38,      11718.8,      15000,        15000,        15000,        
          17000,        20000,        23437.5,      25000,        30000,        
          35000,        40000,        46875,        50000,        60000,        
          70000,        80000,        90000,        93750,        100000,       
          100000,       100000,       125000,       137500,       143750,       
          146875,       148438,       149219,       149609,       149805,       
          149902,       149951,       149976,       149988,       149994,       
          149997,       149998,       149999,       150000,       150000,       
          150000,       150000,       150000,       150000,       150000,       
          150000,       150000,       150000,       187500,       375000,       
          450000,       562500,       600000,       656250,       675000,       
          703125,       712500,       726562,       731250,       738281,       
          740625,       744141,       745312,       747070,       747656,       
          748535,       748828,       749268,       749414,       749634,       
          749707,       749817,       749854,       749908,       749927,       
          749954,       749963,       749977,       749982,       749989,       
          749991,       749994,       749995,       749997,       749998,       
          749999,       749999,       749999,       749999,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       750000,       750000,       750000,       750000,       
          750000,       1.09e+06,     2e+06,        5e+06,        5.5e+06,      
          6e+06,        6.5e+06,      7e+06,        7.5e+06,      8e+06,        
          1.42e+07,     1.9e+07,      1.95e+07,     2e+07,        3e+07
        };

        auto trial = tRECONR::unionizeEnergyGrid( logger, r2d2, energies );

        details::checkRanges( ref, trial );
      } // THEN
    } // GIVEN
  } // WHEN
} // SCENARIO
